-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for ESM tailwind.config.js
files
#3544
Conversation
try { | ||
return require(config) | ||
} catch (e) { | ||
if (e.code === 'ERR_REQUIRE_ESM') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it is a different exception? Right now you will return null
, but I think you should throw the original exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, that makes total sense. I will update this!
if (e.code === 'ERR_REQUIRE_ESM') { | ||
try { | ||
return import(config).then(mdl => mdl.default) | ||
} catch (e) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to my previous comment, maybe just throw the original exception in here if import
also failed, instead of an empty catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@natemoo-re Might you update the pull request, please?
Any update on this? I want to import esm file in
I think this may help if i can change the config file to esm type. |
Any update on this? |
I would really appreciate this feature! |
Hey! So this has been open as a draft for a while now and I'm looking through issues. Looking into this we'd need to make a handful things async (some of which you have done) but this will definitely break intellisense with the way it's currently implemented. Many things there are assumed to be entirely synchronous. We definitely would love to have ESM support (I would love it personally too) for config files but it can't happen in v3.1. The other thing is that Given that this would be a non-trivial time sink for Tailwind CSS, Intellisense, and possibly the prettier plugin, we would need to be sure we could support and update all of those at the same time. Neither I nor the rest of the team have the time to do this properly right now so I'm gonna close this. I really appreciate your work on this and will absolutely reference and credit it in the future when we have a chance to think through this and implement it in a way that we feel comfortable with shipping. Really do appreciate your time on this though! ✨ |
ESM support is more important than intellisense or the prettier plugin. Not having ESM means I won't be using tailwind for my next project. I often don't have intellisense (TM), due to the sea of cloud IDE's with their own flavor of it, so that's not an issue. Anthony Fu has even renounced prettier in favor of other workflows for similar reasons reasons. What I'm saying is, ESM should have been supported Q4 2021, at the latest, and not having it is a really bad look, so it should be priority number one. Off the top of my head, node-fetch, test runner, speed, top-level async and avoiding configuration nightmares are well worth drooping tailwind for UnoCSS or bare-metal PostCSS, depending on whether you can use Vite. |
theres a workaround by using this code for your postcss config: import autoprefixer from 'autoprefixer'
import tailwind from 'tailwindcss'
import tailwindConfig from './tailwind.config.js'
export default {
plugins: [tailwind(tailwindConfig), autoprefixer],
} then your tailwind config file can use esm e.g.: export default {
content: ['./src/components/**/*.jsx'],
theme: {
extend: {},
},
plugins: [],
} credit to @muuvmuuv for this |
ESM adoption in Node is becoming more common—it would be really great for Tailwind to get out ahead of a lot of the issues people are about to run into as the ecosystem shifts to support ESM.
Currently, users who rely on Node ESM with "type": "module" (officially supported since node@12.17.0) must use a
tailwind.config.cjs
file in CJS format (#3181) inside of their ESM project. This is great! But the best solution would be to allow ESM projects to use the normaltailwind.config.js
file in ESM format, as requested in discussion #2284.I thought this would be a bigger problem, but it turned out to be fairly simple. The magic here is a utility function that attempts to
require
the config path (will work for CJS files),catch
any errors withcode: "ERR_REQUIRE_ESM"
, and thenimport
the config path instead (will work for ESM files). Note that this util function is split into a separate file and excluded from thebabel
config, otherwisebabel
transpiles out the nativeimport
statement.Two issues I'd love some assistance on:
getModuleDependencies
and maybe others?